package TestSynchronized;

import org.junit.jupiter.api.Test;

import java.util.concurrent.TimeUnit;

/**
 * @Author Mr.Wen
 * @Version 1.0
 * 死锁案例
 */
public class TestDeadLock {
    public static void main(String[] args) {
        final Object o1 = new Object();
        final Object o2 = new Object();
        new Thread(() -> {
            synchronized (o1) {
                System.out.println("A拿o1锁,想要拿o2锁");
                try {
                    TimeUnit.SECONDS.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (o2) {
                    System.out.println("A拿到了o2锁");
                }
            }
        }, "A").start();

        new Thread(() -> {
            synchronized (o2) {
                System.out.println("B拿o2锁,想要拿o1锁");
                try {
                    TimeUnit.SECONDS.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (o1) {
                    System.out.println("B拿到了o1锁");
                }
            }
        }, "B").start();
    }


}
